package cn.iocoder.yudao.module.system.util.neo4j;

import cn.hutool.core.util.IdUtil;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.system.config.a_gaojian.ConnectionConfig;
import cn.iocoder.yudao.module.system.util.a_gaojian.DateUtils;
import com.trs.hybase.client.TRSConnection;
import com.trs.hybase.client.TRSException;
import com.trs.hybase.client.TRSInputRecord;
import com.trs.hybase.client.TRSReport;
import com.trs.hybase.client.params.OperationParams;
import org.neo4j.driver.v1.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxj
 */
@Service
@Validated
public class EntityRelationServiceImpl implements EntityRelationService {

    /**
     * 表名
     */
    @Value("${hybase.dbname.entityRelation}")
    private String entityRelation;


    @Override
    public void insertEntityRelation(TRSConnection conn, EntityRelationDO entityRelationDO) {
        //创建记录对象
        List<TRSInputRecord> recordList = new ArrayList<>();
        TRSInputRecord record = new TRSInputRecord();
        //添加字段
        try {
            record.addColumn("id", IdUtil.fastSimpleUUID());
            record.addColumn("chapter", entityRelationDO.getChapter());
            record.addColumn("incident", entityRelationDO.getIncident());
            record.addColumn("relation_person", entityRelationDO.getRelationPerson());
            record.addColumn("relation_org", entityRelationDO.getRelationOrg());
            record.addColumn("create_time", DateUtils.formatDateToStr(new Date()));
            record.addColumn("update_time", DateUtils.formatDateToStr(new Date()));
            record.addColumn("book_url", entityRelationDO.getBookUrl());
            record.addColumn("create_user_id", WebFrameworkUtils.getLoginUserId().toString());
            record.addColumn("update_user_id", WebFrameworkUtils.getLoginUserId().toString());
            record.addColumn("content", entityRelationDO.getContent());
            record.addColumn("entity", entityRelationDO.getEntity());
            record.addColumn("bookName", entityRelationDO.getBookName());
            recordList.add(record);
            //执行插入记录（暂时在内存中）
            conn.executeInsert(entityRelation, recordList);
        } catch (TRSException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Boolean updateEntityRelation(TRSConnection conn, EntityRelationDO entityRelationDO) {

        //创建与服务器的连接实例
        List<TRSInputRecord> newValues = new ArrayList<>();
        OperationParams params = new OperationParams();
        TRSReport trsReport = new TRSReport();
        params.setProperty("unique.column.search", "true");
        params.setProperty("unique.column", "id");
        try {
            TRSInputRecord record = new TRSInputRecord();
            record.addColumn("id", entityRelationDO.getId());
            record.addColumn("relation_person", entityRelationDO.getRelationPerson());
            record.addColumn("relation_org", entityRelationDO.getRelationOrg());
            record.addColumn("update_time", DateUtils.formatDateToStr(new Date()));
            record.addColumn("update_user_id", entityRelationDO.getUpdateTime());
            newValues.add(record);
            conn.executeUpdate(entityRelation, newValues, params, trsReport);
        } catch (TRSException e) {
            e.printStackTrace();
        }
        return trsReport.getUpdatedNum() > 0;
    }


}
